import Vue from "vue";
import {
	setToken,
	setUserInfo,
	getToken,
	getConn
} from "@/store/user";
let bon = false;
export const fetch = (method, url, data, params, noToken = true, showTitle) =>
	new Promise(async (resolve, reject) => {
		const that = this;
		const token = getToken();
		let obj = {
			Accept: "application/json",
			"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
			"content-type": "application/json",
		};
		if (
			noToken &&
			url.indexOf("/basic/token/login") === -1 &&
			url.indexOf("/basic/captchaImage") === -1
		) {
			obj["Token"] = token;
		}
		let arr = [];
		if (method === "get") {
			params = params ? params : data;
		}
		for (const key in params) {
			if (Object.hasOwnProperty.call(params, key)) {
				const element = params[key];
				if (element) arr.push(`${key}=${element}`);
			}
		}

		let baseUrl = '';
		// #ifdef H5
		// 如需跨域参照以下h5跨域配置
		// baseUrl = ""
		baseUrl = "/api"
		// #endif

		// #ifdef APP-PLUS
		baseUrl = getConn().mes;
		// #endif
		console.log('fetch url =>', `${baseUrl}${url}${arr.length > 0 ? "?" + arr.join("&") : ""}`);
		uni.request({
			method,
			timeout: 20000,
			url: `${baseUrl}${url}${arr.length > 0 ? "?" + arr.join("&") : ""}`,
			header: obj,
			data: data,
			success(res) {
				switch (res.data.code) {
					case 2003:
					case 401:
						if (bon) return;
						bon = true;
						uni.$emit("openToast", {
							type: "error",
							icon: "error",
							title: "身份认证过期!",
							complete() {
								setToken("");
								setUserInfo("");
								setTimeout(() => {
									uni.reLaunch({
										url: "/pages/login/index",
										animationType: "pop-in",
									});
								}, 1000);
							},
						});
						bon = false;
						return resolve(res.data);
						break;
					default:
						break;
				}
				if (res.data.code == "200") {
					Vue.prototype.$loading.close();
					return resolve(res.data);
				} else if (res.data.code == "1" && res.data.msg) {
					Vue.prototype.$loading.close();
					if (showTitle) {
						// 特殊请求 code==1 需要交互操作
						return resolve(res.data);
					} else {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: function(res) {}
						});
					}
				} else if (res.code == "1" && res.msg) {
					Vue.prototype.$loading.close();
					uni.showModal({
						title: '提示',
						content: res.msg ? res.msg : "操作失败",
						showCancel: false,
						success: function(res) {}
					});

				} else {
					return reject(res.data);
				}
			},
			fail(err) {
				return reject(err);
			},
			complete() {},
		});
	}).catch((res) => {
		Vue.prototype.$loading.close();
		console.log("res", res);
		// uni.$emit("openToast", {
		// 	type: "error",
		// 	icon: "error",
		// 	title: res.msg ? res.msg : "操作失败", //app 只能显示7个汉字
		// });
		uni.showModal({
			title: '提示',
			content: res.msg ? res.msg : "操作失败",
			showCancel: false,
			success: function(res) {}
		});
		return Promise.reject({});
	});
export default fetch;